from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field

# 聊天目标类型
class ChatGoalType:
    SOCIAL = "social"  # 社交
    WORK_REPORT = "work_report"  # 工作汇报
    NEGOTIATION = "negotiation"  # 协商
    INFORMATION = "information"  # 信息获取
    PROBLEM_SOLVING = "problem_solving"  # 问题解决
    EMOTIONAL_SUPPORT = "emotional_support"  # 情感支持
    OTHER = "other"  # 其他


# 情感分析结果
class EmotionAnalysis(BaseModel):
    positive: float = Field(..., ge=0, le=1, description="积极情绪分数")
    negative: float = Field(..., ge=0, le=1, description="消极情绪分数")
    neutral: float = Field(..., ge=0, le=1, description="中性情绪分数")
    dominant_emotion: str = Field(..., description="主导情绪")


# 语气分析结果
class ToneAnalysis(BaseModel):
    formal: float = Field(..., ge=0, le=1, description="正式程度")
    friendly: float = Field(..., ge=0, le=1, description="友好程度")
    aggressive: float = Field(..., ge=0, le=1, description="攻击性程度")
    submissive: float = Field(..., ge=0, le=1, description="顺从程度")
    confident: float = Field(..., ge=0, le=1, description="自信程度")
    dominant_tone: str = Field(..., description="主导语气")


# 主题分析结果
class TopicAnalysis(BaseModel):
    topics: List[Dict[str, float]] = Field(..., description="主题及其权重")
    main_topic: str = Field(..., description="主要主题")


# 完整的聊天分析结果
class ChatAnalysis(BaseModel):
    emotion: EmotionAnalysis
    tone: ToneAnalysis
    topic: TopicAnalysis
    word_count: int
    avg_response_time: Optional[float] = None  # 平均回复时间(秒)
    relationship_assessment: Optional[str] = None  # 关系评估
    goal_achievement: Optional[float] = None  # 目标达成度(0-1)


# 优化建议请求
class OptimizationRequest(BaseModel):
    text: str = Field(..., description="待优化的文本")
    contact_id: Optional[int] = None
    session_id: Optional[int] = None
    relationship_type: str = Field(default="friend", description="关系类型")
    chat_goal: str = Field(default="social", description="聊天目标")
    custom_goal: Optional[str] = None
    context_messages: Optional[List[int]] = None  # 上下文消息ID列表


# 单条优化建议
class OptimizationSuggestion(BaseModel):
    original_text: str
    suggested_text: str
    explanation: str
    modification_points: List[Dict[str, Any]]
    expected_effect: Dict[str, float]
    confidence: float = Field(..., ge=0, le=1, description="建议的置信度")


# 优化建议响应
class OptimizationResponse(BaseModel):
    original_text: str
    suggestions: List[OptimizationSuggestion]
    analysis: ChatAnalysis


# PUA检测结果
class PUADetectionResult(BaseModel):
    is_pua: bool
    score: float = Field(..., ge=0, le=1, description="PUA评分")
    detected_patterns: List[Dict[str, Any]]
    risk_level: str  # low, medium, high
    explanation: str
    response_suggestions: List[str]